package com.ceres.login.controller;

import com.ceres.entity.Result;
import com.ceres.login.pojo.User;
import com.ceres.login.service.UserService;
import com.ceres.login.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping("/user")
@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public Result login(String username,String password) {
        int result = userService.login(username,password);
        System.out.println(result);
        if (result == 1) {
            //密码是正确
            //生成jwt令牌,返回到客户端
            Map<String, String> info = new HashMap<>();
            info.put("username", username);
            //基于工具类生成jwt令牌
            String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(), username, null);
            info.put("token", jwt);
            return new Result(true, 2000, info);
        } else if (result == 3) {
            //账号被锁定
            return new Result(false, 3003);
        } else if (result == 4) {
            //初次登录
            return new Result(true, 3004);
        }else if (result==5){
            //账号到期
            return new Result(false,3005);
        } else {
            //账号密码错误
            return new Result(false, 3002);
        }
    }
    //根据用户名查询用户
    @GetMapping("/load/{username}")
    public User findUserInfo(@PathVariable("username") String username){
        return userService.findByUsername(username);
    }
    //账号被锁定
    @GetMapping("/edit/{username}")
    public void editAdminStatus(@PathVariable("username") String username){
        userService.editUserStatus(username);
    }
    //查询角色
    @GetMapping("/findRoleByUsername/{username}")
    public List<Integer> findRoleByUsername(@PathVariable("username") String username){
        return userService.findRoleByUsername(username);
    }
    @GetMapping("/findDateByusername/{username}")
    public Date findDateByusername(@PathVariable("username") String username){
        return userService.findDateByusername(username);
    }
}
